package com.itheima.leyou.dao;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Map;

@Repository
public class StockDao {

    @Resource
    private JdbcTemplate jdbcTemplate;

    public ArrayList<Map<String, Object>> getStockList(){
        //1、创建sql语句
        String sql = "SELECT id AS sku_id, title, images, stock, price, indexes, own_spec " +
                "FROM tb_sku";
        //2、执行sql语句
        return (ArrayList<Map<String, Object>>) jdbcTemplate.queryForList(sql);
    }

    public ArrayList<Map<String, Object>> getStock(String sku_id){
        //1、创建SQL语句
        String sql = "select tb_sku.spu_id, tb_sku.title, tb_sku.images, tb_sku.stock, tb_sku.price, tb_sku.indexes, " +
                "tb_sku.own_spec, tb_sku.enable, tb_sku.create_time, tb_sku.update_time,tb_spu_detail.description," +
                "tb_sku.id AS sku_id,tb_spu_detail.special_spec " +
                "from tb_sku " +
                "INNER JOIN tb_spu_detail ON tb_spu_detail.spu_id=tb_sku.spu_id " +
                "where tb_sku.id = "+sku_id;;
        //2、执行SQL语句
        return (ArrayList<Map<String, Object>>) jdbcTemplate.queryForList(sql);
    }


    public boolean insertLimitPolicy(Map<String, Object> map){
        //1、创建SQL语句
        String sql = "insert into tb_limit_policy (sku_id, quanty, price, begin_time, end_time) " +
                "Values (?, ?, ?, ?, ?)";

        //2、执行SQL语句
        return jdbcTemplate.update(sql, map.get("sku_id"), map.get("quanty"),
                map.get("price"), map.get("begin_time"), map.get("end_time"))==1;
    }
}
